package mobile.TheOldRepublic.web;

import android.app.Activity;
import android.app.ProgressDialog;
import android.util.AttributeSet;
import android.webkit.WebView;


public class WebViewWrapper extends WebView{
	
	private static final String JAVASCRIPT_TOOLS = "ittrTools";
	
	protected WebViewClientWrapper webViewClient = new WebViewClientWrapper();

	public WebViewWrapper(Activity context) {
		super(context);
		init(context);
	}
	
	public WebViewWrapper(Activity context, AttributeSet attrs){
		super(context, attrs);
		init(context);
	}
	
	public WebViewWrapper(Activity context, AttributeSet attrs, int defStyle){
		super(context,attrs,defStyle);
		init(context);
	}
	
	public void init(Activity activity){
		this.getSettings().setJavaScriptEnabled(true);
		this.addJavascriptInterface(new JavascriptTools(activity, this), JAVASCRIPT_TOOLS);
		webViewClient.setActivity(activity);
		this.setWebViewClient(webViewClient);
	}
	
	/**
	 * Because the default loadUrl does not call shouldOverrideUrlLoading I'm overriding it.
	 */
	@Override
	public void loadUrl(String url){
		boolean override = false;
		override = this.webViewClient.shouldOverrideUrlLoading(this, url);
		
		if(!override)
			super.loadUrl(url);
	}
	
	/**
	 * Package level method for loading urls without going through shouldOverrideUrlLoading.
	 */
	void internalLoadUrl(String url){
		super.loadUrl(url);
	}
	
	/**
	 * Moving functionality to WebViewWrapper so that content isn't reparsed.
	 */
	@Override
	public void goBack(){
		webViewClient.goBack();
	}

	/**
	 * Moving functionality to WebViewWrapper so that content isn't reparsed.
	 */
	@Override
	public boolean canGoBack(){
		return webViewClient.canGoBack();
	}
	
	public void login(CharSequence username, CharSequence password){
		StringBuilder js = new StringBuilder();
		js.append("javascript:");
		// Move the error message block where people can see it. LP
		js.append("$('#header').append($('#loginInfo'));");

		js.append("$('#name').val('"+username+"');");
		js.append("$('#pass').val('"+password+"');");
//		js.append("$('.loginInvalidPassword').css('display', 'block');"); //debug
//		js.append("$('.loginInvalidPassword').text('Test error message.');"); //debug
		js.append("$('#login_submit').click();");
		
		// This is kind of a hack, but I don't see a way to capture an event from another js action. LP
		js.append("setTimeout(function(){"+JAVASCRIPT_TOOLS+".displayMessage($('.loginInvalidPassword').text());}, 1000);");
		
//		Log.d(getClass().getSimpleName(), "javascript = "+js.toString());
		
		internalLoadUrl(js.toString());
	}
	
	public void forgotPassword(){
		StringBuilder js = new StringBuilder();
		js.append("javascript:");
		js.append("window.open($('.loginForgotPassword a').attr('href'));"); //debug
		
		internalLoadUrl(js.toString());
	}
	
	//Debug
	public String getDebuggingHtml(){
		return webViewClient.getLastHtml();
	}
	
	public void setProgressDialog(ProgressDialog pd){
		webViewClient.setProgressDialog(pd);
	}
	
	public Activity getActivity(){
		return webViewClient.getActivity();
	}
	public void setActivity(Activity a){
		webViewClient.setActivity(a);
	}
}